package com.youlai.boot.modules.country.controller;

import com.youlai.boot.modules.country.service.CountryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.country.model.form.CountryForm;
import com.youlai.boot.modules.country.model.query.CountryQuery;
import com.youlai.boot.modules.country.model.vo.CountryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 国家数据前端控制层
 *
 * @author liuwanwei
 * @since 2025-02-26 17:48
 */
@Tag(name = "国家数据接口")
@RestController
@RequestMapping("/api/v1/countrys")
@RequiredArgsConstructor
public class CountryController  {

    private final CountryService countryService;

    @Operation(summary = "国家数据分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('module:country:query')")
    public PageResult<CountryVO> getCountryPage(CountryQuery queryParams ) {
        IPage<CountryVO> result = countryService.getCountryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增国家数据")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('module:country:add')")
    public Result<Void> saveCountry(@RequestBody @Valid CountryForm formData ) {
        boolean result = countryService.saveCountry(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取国家数据表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('module:country:edit')")
    public Result<CountryForm> getCountryForm(
        @Parameter(description = "国家数据ID") @PathVariable Long id
    ) {
        CountryForm formData = countryService.getCountryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改国家数据")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('module:country:edit')")
    public Result<Void> updateCountry(
            @Parameter(description = "国家数据ID") @PathVariable Long id,
            @RequestBody @Validated CountryForm formData
    ) {
        boolean result = countryService.updateCountry(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除国家数据")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('module:country:delete')")
    public Result<Void> deleteCountrys(
        @Parameter(description = "国家数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = countryService.deleteCountrys(ids);
        return Result.judge(result);
    }
}
